package com.cybertron.ironhide.spider.plugin.impl;

import java.util.ArrayList;
import java.util.List;

import org.eweb4j.util.CommonUtil;

import com.cybertron.ironhide.spider.SpiderListener;
import com.cybertron.ironhide.spider.domain.TaskUrl;
import com.cybertron.ironhide.spider.plugin.DupRemovalPoint;
import com.cybertron.ironhide.spider.plugin.duplicate.DocIDServer;
import com.cybertron.ironhide.spider.plugin.util.Util;
import com.cybertron.ironhide.spider.task.Task;
import com.cybertron.ironhide.spider.xml.Site;
import com.cybertron.ironhide.spider.xml.Target;

public class DupRemovalPointImpl implements DupRemovalPoint {

	private SpiderListener listener;

	private Site site;

	public void init(Site site, SpiderListener listener) {
		this.site = site;
		this.listener = listener;
		if (this.site.db == null) {
			this.site.db = new DocIDServer(site.getName(), listener);
			listener.onInfo(Thread.currentThread(), null, "DocIDServer -> "
					+ site.getName() + " initial success...");
		}
	}

	public void destroy() {
		if (this.site.db != null) {
			this.site.db.close();
			this.site.db = null;
			listener.onInfo(Thread.currentThread(), null, "DocIDServer -> "
					+ site.getName() + " destroy success...");
		}
	}

	public synchronized List<Task> removeDuplicateTask(Task task,
			List<TaskUrl> newUrls) {
		if (task.site.db == null)
			return null;

		List<Task> validTasks = new ArrayList<Task>();
		for (TaskUrl taskUrl : newUrls) {
			Task newTask = new Task(task.seedUrl, taskUrl.getUrl(), task.url,
					taskUrl.getIsSource(), task.site, 10);
			try {
				Target tgt = Util.matchTarget(newTask);
				// 默认是严格限制重复URL的访问，只要是重复的URL，都只能访问一次
				String docKey = CommonUtil.md5(newTask.url);

				// 如果配置的是不严格限制重复URL的访问，则将去重复判断的key变成 TargetUrl + SourceUrl
				// 这样就表示不同来源的TargetUrl，就算相同，也是可以访问的
				String isStrict = task.site.getIsDupRemovalStrict();
				if ("0".equals(isStrict) && tgt != null) {
					docKey = CommonUtil.md5(newTask.url + newTask.sourceUrl);
				}

				int docId = task.site.db.getDocId(docKey);
				if (docId < 0) {
					validTasks.add(newTask);
					task.site.db.newDocID(docKey);
				}
			} catch (Exception e) {
				listener.onError(Thread.currentThread(), newTask, "", e);
			}
		}

		return validTasks;
	}
}
